package ltd.starriddle.smart4j.chapter3.controller;

import ltd.starriddle.smart4j.chapter3.model.Customer;
import ltd.starriddle.smart4j.chapter3.service.CustomerService;
import ltd.starriddle.smart4j.framework.annotation.Action;
import ltd.starriddle.smart4j.framework.annotation.Controller;
import ltd.starriddle.smart4j.framework.annotation.Inject;
import ltd.starriddle.smart4j.framework.bean.Param;
import ltd.starriddle.smart4j.framework.bean.View;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 处理客户管理相关请求
 *
 * @author CYL
 * @since 2020-12-27
 */
@Controller
public class CustomerController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);

    @Inject
    private CustomerService customerService;

    /**
     * 进入 客户列表 界面
     * @param param
     * @return
     */
    @Action("get:/customer")
    public View index(Param param) {
        LOGGER.info("req 客户列表");
        List<Customer> customerList = customerService.getCustomerList();
        LOGGER.info("resp 客户列表：{}", customerList);
        return new View("customer.jsp").addModel("customerList", customerList);
    }

    /**
     * 显示客户基本信息
     * @param param
     * @return
     */
    @Action("get:/customer_show")
    public View show(Param param) {
        long id = param.getLong("id");
        LOGGER.info("req 客户详情 id={}", id);
        Customer customer = customerService.getCustomer(id);
        LOGGER.info("resp 客户详情 customer={}", customer);
        return new View("customer_show.jsp").addModel("customer", customer);
    }

    /**
     * 进入 创建客户 界面
     * @param param
     * @return
     */
    @Action("get:/customer_create")
    public View create(Param param) {
        LOGGER.info("req 创建客户 界面");
        return new View("customer_create.jsp");
    }

    /**
     * 处理 创建客户 请求
     * @param param
     * @return
     */
    @Action("post:/customer_create")
    public View createSubmit(Param param) {
        Map<String, Object> fieldMap = param.getMap();
        LOGGER.info("req 创建客户 处理 fieldMap={}", fieldMap);
        boolean result = customerService.createCustomer(fieldMap);
        LOGGER.info("resp 创建客户 处理 result={}", result);
        return new View("customer_create.jsp").addModel("result", result);
    }

    /**
     * 进入 编辑客户 界面
     * @param param
     * @return
     */
    @Action("get:/customer_edit")
    public View edit(Param param) {
        long id = param.getLong("id");
        LOGGER.info("req 编辑客户 界面 id={}", id);
        Customer customer = customerService.getCustomer(id);
        LOGGER.info("resp 编辑客户 界面 customer={}", customer);
        return new View("customer_edit.jsp").addModel("customer", customer);
    }

    /**
     * 处理 编辑客户 请求
     * @param param
     * @return
     */
    @Action("post:/customer_edit")
    public View editSubmit(Param param) {
        long id = param.getLong("id");
        LOGGER.info("req 编辑客户 处理 id={} fieldMap={}", id, param.getMap());

        Customer customer = customerService.getCustomer(id);
        boolean result = false;
        String msg = "修改失败";
        if (customer == null) {
            msg = "客户不存在";
        } else if (StringUtils.isAnyBlank(param.getString("name"), param.getString("contact"))) {
            msg = "名称 和 联系人 不能为空";
        } else  {
            result = customerService.updateCustomer(id, param.getMap());
            if (result) {
                result = true;
                msg = "修改成功";
                customer = customerService.getCustomer(id);
            }
        }
        LOGGER.info("resp 编辑客户 处理 result={} msg={} customer={}", result, msg, customer);
        return new View("customer_edit.jsp").addModel("result", result).addModel("msg", msg).addModel("customer", customer);
    }

    /**
     * 进入 删除客户 界面
     * @param param
     * @return
     */
    @Action("get:/customer_delete")
    public View delete(Param param) {
        long id = param.getLong("id");
        LOGGER.info("req 删除客户 界面 id={}", id);
        Customer customer = customerService.getCustomer(id);
        LOGGER.info("resp 删除客户 界面 customer={}", customer);
        return new View("customer_delete.jsp").addModel("customer", customer);
    }

    /**
     * 处理 删除客户 请求
     * @param param
     * @return
     */
    @Action("post:/customer_delete")
    public View deleteSubmit(Param param) {
        long id = param.getLong("id");
        LOGGER.info("req 删除客户 处理 id={}", id);

        boolean result = false;
        String msg = "删除失败";
        Customer customer = customerService.getCustomer(id);
        if (customer == null) {
            msg = "客户不存在";
        } else {
            result = customerService.deleteCustomer(id);
            if (result) {
                result = true;
                msg = "删除成功";
            }
        }
        LOGGER.info("resp 删除客户 处理 id={} result={} customer={}", id, result, customer);
        return new View("customer_delete.jsp").addModel("customer", customer).addModel("result", result).addModel("msg", msg);
    }
}
